package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.support.v7.widget.ListPopupWindow;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.base.BaseDialog;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.OrderCancelModel;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupListAdapter;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupWindowHelper;
import com.wushi.lenovo.asia5b.app_utils.helper.CacheHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/1/24.
 */
public class OrderCancelDialog extends BaseDialog {
    @BindView(R.id.iv_cancelsure_ok)
    ImageView iv_cancelsure_ok;
    @BindView(R.id.tv_order_item)
    TextView tv_order_item;
    @BindView(R.id.tv_cancelsure_sure)
    TextView tv_cancelsure_sure;
    @BindView(R.id.tv_cancelsure_cancel)
    TextView tv_cancelsure_cancel;
    @BindView(R.id.et_order_remark)
    EditText et_order_remark;
    @BindView(R.id.ll_order_remark)
    LinearLayout ll_order_remark;
    @BindView(R.id.tv_popup_content)
    TextView tv_popup_content;
    @BindView(R.id.tv_popup_content_title_explain)
    TextView tv_popup_content_title_explain;
    private ListPopupWindow popupWindowBank;
    private PopupListAdapter<OrderCancelModel> adapter;
    private final OrderGoodBean.ActionBean actionBean;
    private final BaseAppCompatActivity activity;
    private final List<OrderCancelModel> mOrderCancelDatas = new ArrayList<>();

    public static class CancelBean {
        public int cancel_status;
        public String reason;
    }


    public OrderCancelDialog(BaseAppCompatActivity activity, OrderGoodBean.ActionBean actionBean) {
        super(activity);
        this.activity = activity;
        adapter = new PopupListAdapter(getContext(), R.layout.item_popupitem_adapter, mOrderCancelDatas);
        this.actionBean = actionBean;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_order_cancel;
    }

    @Override
    protected void initView() {
        tv_popup_content.setText(actionBean.getPopup_content());
        tv_popup_content_title_explain.setText(actionBean.getPopup_content_title_explain());
        et_order_remark.setHint(actionBean.getPopup_content_ph_explain());
//        tv_cancelsure_sure.setText(R.string.submit);
//        tv_cancelsure_cancel.setText(R.string.cancel);
        tv_cancelsure_sure.setText(actionBean.getPopup_button_left().getName());
        tv_cancelsure_cancel.setText(actionBean.getPopup_button_right().getName());


        popupWindowBank = PopupWindowHelper.getListPopupWindow(getContext(), adapter
                , tv_order_item, ListPopupWindow.WRAP_CONTENT);

        CacheHelper.getOrderCancelContents(activity, orderCancelModels1 -> {
            if (orderCancelModels1 != null && orderCancelModels1 != null) {
                mOrderCancelDatas.clear();
                mOrderCancelDatas.addAll(orderCancelModels1);
                adapter.notifyDataSetChanged();
                adapter.setSelectPosition(0);
                tv_order_item.setText(adapter.getSelectItemText());
            }
        });
    }


    @Override
    protected void initListener() {
        popupWindowBank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectPosition(position);
                popupWindowBank.dismiss();
                OrderCancelModel model = adapter.getItem(position);
                if (model != null) {
                    tv_order_item.setText(model.getName());
                    ll_order_remark.setVisibility(model.getId() == 3 ? View.VISIBLE : View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.iv_cancelsure_ok, R.id.tv_cancelsure_sure, R.id.tv_cancelsure_cancel
            , R.id.tv_order_item})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancelsure_ok: {
                dismiss();
            }
            break;
            case R.id.tv_cancelsure_sure: {
                CancelBean bean = new CancelBean();
                if (callBack != null) {
                    OrderCancelModel orderCancelModel = adapter.getSelectItem();
                    if (orderCancelModel != null) {
                        bean.cancel_status = orderCancelModel.getId();
                        if (bean.cancel_status == 3) {
                            bean.reason = et_order_remark.getText().toString().trim();
                            if (TextUtils.isEmpty(bean.reason)) {
                                ToastUtil.showToast(getString(R.string.orderitem_cancelorder));
                                return;
                            }
                        }
                    }
                    callBack.callback(bean);
                }
            }
            dismiss();
            break;
            case R.id.tv_cancelsure_cancel: {
                dismiss();
            }
            break;
            case R.id.tv_order_item: {
                PopupWindowHelper.showPopupWindow(popupWindowBank);
            }
            break;
            default:
                break;
        }

    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.7f);
    }
}
